<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Property extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		// load library
		// load helper
		$this->load->helper(array('url', 'form', 'html'));
		// load model
		$this->load->model('Propertycatg_model','',TRUE);
		$this->load->model('Property_model','',TRUE);
	}
	
	// load index file
	function index($offset=0) {
		$data['title'] = 'Property listings and fetaures';
		$data['message'] = '';
		$data['action'] = 'details';
		
		//get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Property_model->get_result($cond, $limit, $offset, $sort);
		$data['plist'] = $results['rows'];
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['num_rows'] = $results['num_rows'];
		$data['active'] = 'property';
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('property/index');
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		$this->load->view('property/index', $data);
	
	}
	
	function details($id)
	{
		$data['title'] = 'Property listings and fetaures';
		$data['message'] = '';
		$c_id = (int) $id;
		
		$cond['propertyID'] = $c_id;
		$results =$this->Property_model->get_result($cond);
		$plist = $results['rows'];
		$property = array();
		
		foreach ($plist as $p)
		{
			$property = $p;
		}
		$otherCond['is_active'] = 1;
		$otherCond['advertiserID'] = $property->advertiserID;
		$otherads_result = $this->Property_model->get_result($otherCond, 4);
		$otherads = $otherads_result['rows'];
		//$auto_details = $autolist['rows'][0];
		$data['backurl'] = 'property/';
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['active'] = 'property';
		$data['property'] = $property;
		$data['others'] = $otherads;
		$data['bookmark'] = $property->title;
		$this->load->view('property/details', $data);
	}
	
	//display based on browsing 
	function browse($id = 1,$offset=0)
	{
		$catgID = (int) $id;
		
		$cond['typeID'] = $catgID;
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		$data['active'] = 'property';
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Property_model->get_result($cond, $limit, $offset, $sort);
		$data['plist'] = $results['rows'];
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['num_rows'] =$results['num_rows'];
		$data['active'] = 'property';
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('property/browse/') . '/'. $catgID . '/';
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		//to display which category was chosen
		$data['selectedcatg'] = $data['catg'][$catgID];
		$data['bookmark'] = $data['catg'][$catgID];
		$this->load->view('property/index', $data);
	
	}

	/**
	* advanced search 
	* This function is called when user submit 'search' form
	**/
	function search()
	{
		
		$keyword=0; $offset=0;$city = 0; $bedrm=0;$bathrm = 0;
		if($this->input->post('keyword')){
			$keyword = $this->input->post('keyword');
			$data['keyword'] = $keyword;
		}
		$othercond = NULL; $cond = NULL;
		if ($this->input->post('searchCity')){
			$cond['city'] = $this->input->post('searchCity');
			$city = $cond['city'];
			$data['city'] = $city;
		}
		$propertyType = 0;
		if ($this->input->post('dropdown_type')){
			$cond['typeID'] = $this->input->post('dropdown_type');
			$propertyType = $cond['typeID'];
			$data['propertyType'] = $propertyType;
		}

		if ($this->input->post('dropdown_bedrooms')){
			$cond['bed_room'] = $this->input->post('dropdown_bedrooms');
			$bedrm = $cond['bed_room'];
			$data['bedrm'] = $bedrm;
		}

		if ($this->input->post('dropdown_bathrooms')){
			$cond['bath_room'] = $this->input->post('dropdown_bathrooms');
			$bathrm = $cond['bath_room'];
			$data['bathrm'] = $bathrm;
		}

		$limit = $this->config->item('per_page');
		$cond['is_active'] = 1;
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		
		$results = $this->Property_model->get_search_result($keyword,$cond, $limit, $offset, $sort, $othercond);
		$data['plist'] = $results['rows'];
		
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['num_rows'] = $results['num_rows'];
		$data['active'] = 'property';//for menu

		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('property/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $propertyType
							  . '/' . $city . '/'. $bedrm . '/'. $bathrm . '/' ;
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		$data['bookmark'] = 'Search';
		$this->load->view('property/index', $data);
		
	}
	/**
	* advanced search 
	* This function is subsequent calling function for 
	**/
	function result( $keyword = '', $propertyType=0, $city=0, $bedrm = 0, $bathrm = 0 , $offset=0)
	{
		$cond['is_active'] = 1;
		$othercond = NULL;
		if ($keyword != 0){
			$keyword = str_replace('_', '', $keyword);
			$data['keyword'] = $keyword;

		}

		if ($city != 0){
			$cond['city'] = str_replace('_', '', $city);
			$data['city'] = $cond['city'];
		}
		if ($propertyType > 0) {
			$cond['typeID'] = $propertyType;
			$data['type'] = $cond['typeID'];
		}
		if($bedrm > 0){
			$cond['bed_room'] = $bedrm;
			$data['bedrm'] = $cond['bed_room'];
		}
		if($bathrm > 0) {
			$cond['bath_room'] = $bathrm;
			$data['bathrm'] = $cond['bath_room'];
		}

		$limit = $this->config->item('per_page');
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Property_model->get_search_result($keyword,$cond, $limit, $offset, $sort, $othercond);

		$data['plist'] = $results['rows'];
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['num_rows'] = $results['num_rows'];
		$data['active'] = 'classifieds';//for menu

		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('property/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $propertyType
							  . '/' . $city . '/'. $bedrm . '/'. $bathrm . '/' ;
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 6;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		$data['bookmark'] = 'Search';
		$this->load->view('property/index', $data);
	}
	//display based on filter 
	function filter($keyword = 1,$offset=0)
	{
		
		$cond['categoryID'] = $keyword;
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		$data['active'] = 'property';
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Property_model->get_result($cond, $limit, $offset, $sort);
		$data['plist'] = $results['rows'];
		$data['catg'] = $this->Propertycatg_model->get_property_categories();
		$data['num_rows'] =$results['num_rows'];
		$data['active'] = 'property';
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('property/filter/') . '/'. $keyword . '/';
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		//to display which category was chosen
		//$data['selectedcatg'] = $data['catg'][$catgID];
		$data['bookmark'] = $this->getCategoryName((int)$keyword);
		$this->load->view('property/index', $data);
	
	}
	function getCategoryName ($choice = 0)
	{
		switch($choice){
			case 1:
				return 'Sell';break;
			case 2:
				return 'Buy';break;
			case 3:
				return 'Rent';break;
			case 4:
				return 'Long tenture';break;
			default:
				break;
		}
	}
}